﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TraineeManagement.Models;
using TraineeManagement.Models.BusinessObject;

namespace TraineeManagement.Controllers
{
    public class SessionController : Controller
    {
        private SessionBO SBO = new SessionBO();

        [HttpPost]
        public ActionResult Add(Session session)
        {
            try
            {
                if (!(session.StartDate == DateTime.MinValue) && !(session.EndDate == DateTime.MinValue))
                {
                    SBO.AddSession(session);
                }
                return RedirectToAction("TraineeInfo/" + session.TraineeID, "Trainee");
            }
            catch (Exception)
            {

                return RedirectToAction("Index", "Error");
            }
        }

        [HttpPost]
        public ActionResult Update(Session session)
        {
            try
            {
                Session OriSession = SBO.GetSessionById(session.SessionID);
                if (!(session.StartDate == DateTime.MinValue) && !(session.EndDate == DateTime.MinValue))
                {
                    OriSession.StartDate = session.StartDate;
                    OriSession.EndDate = session.EndDate;
                }
                SBO.UpdateSession(OriSession);
                return RedirectToAction("TraineeInfo/" + session.SessionID, "Trainee");
            }
            catch (Exception)
            {

                return RedirectToAction("Index", "Error");
            }
        }


        public ActionResult Delete(int id)
        {
            try
            {
                Session Session = SBO.GetSessionById(id);
                int traineeID = Session.TraineeID;
                SBO.DeleteSession(Session);
                return RedirectToAction("TraineeInfo/" + traineeID, "Trainee");
            }
            catch (Exception)
            {

                return RedirectToAction("Index", "Error");
            }
        }

    }
}
